﻿using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Users;

namespace Yyd.OrganizationUnits
{
    public abstract class CurrentOrganizationUnitBase : ICurrentOrganizationUnit
    {
        protected OrganizationUnitLevel OrganizationUnitLevel;

        protected readonly IAbpLazyServiceProvider LazyServiceProvider;
        protected ICurrentUser CurrentUser => LazyServiceProvider.LazyGetService<ICurrentUser>();

        /// <summary>
        /// TODO 暂时写死 方便测试
        /// </summary>
        public Guid? Id => CurrentUser.OrganizationUnitId();


        protected CurrentOrganizationUnitBase(IServiceProvider serviceProvider)
        {
            LazyServiceProvider = serviceProvider.GetRequiredService<IAbpLazyServiceProvider>();
        }

        /// <summary>
        /// 获取层级中的第一个门店
        /// </summary>
        /// <returns></returns>
        public virtual async Task<IOrganizationUnit> GetSchoolAsync()
        {
            return (await GetOrganizationUnitLevelAsync())?.GetSchool();
        }

        /// <summary>
        /// 获取层级中的第一个商户
        /// </summary>
        /// <returns></returns>
        public virtual async Task<IOrganizationUnit> GetAreaAsync()
        {
            return (await GetOrganizationUnitLevelAsync())?.GetArea();
        }

        /// <summary>
        ///  获取层级中的第一个公司
        /// </summary>
        /// <returns></returns>
        public virtual async Task<IOrganizationUnit> GetCompanyAsync()
        {
            return (await GetOrganizationUnitLevelAsync())?.GetCompany();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public abstract Task<OrganizationUnitLevel> GetOrganizationUnitLevelAsync();
    }
}
